package com.wondersgroup.fjzlpt.console.service.impl.yljgry;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.quick.framework.util.ExcelOrWordUtils;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.console.dao.inter.ksxx.KsxxDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.user.UserDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryPracticeDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryPracticeMultiDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryZybjDAO;
import com.wondersgroup.fjzlpt.console.service.inter.yljgry.YljgryService;
import com.wondersgroup.fjzlpt.console.utils.FileUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class YljgryServiceImpl implements YljgryService{

	@Autowired
	private YljgryDAO yljgryDAO;
	
	@Autowired
	private KsxxDAO ksDAO;

	@Autowired
	private UserDAO userDAO;

	@Autowired
	/**职业背景*/
	private YljgryZybjDAO yljgryZybjDAO;

	@Autowired
	/**执业信息*/
	private YljgryPracticeDAO yljgryPracticeDAO;

	@Autowired
	/**多点执业信息*/
	private YljgryPracticeMultiDAO yljgryPracticeMultiDAO;
	/**mongo文件操作*/
	@Autowired
	private FileUtils fileUtils;

	/**
	 * 分页查询人员信息
	 * @param map
	 * @return
	 */
//	@Cacheable("queryPageYljgryxx")
	public JSONObject queryPage(Map<String, String> map)
	{
		return this.yljgryDAO.queryPage(map);
	}

	/**
	 * 查询用户信息
	 * @param userId
	 * @return
	 */
	public JSONObject queryYljgryxx(String userId){
		JSONObject yljgryxx = this.yljgryDAO.queryYljgryxx(userId);
		JSONArray ddzyxx = this.yljgryPracticeMultiDAO.queryByUserId(userId);
		yljgryxx.put("ddzyxx", ddzyxx);
		return yljgryxx;
	}

	/**
	 * 获取人员照片
	 * @param id
	 * @return
	 */
	public byte[] receiveImage(String id){
		try{
			return fileUtils.receiveImage("RYZP", id);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("获取人员照片失败", e);
		}
	}

	/**
	 * 判断新增或更新医疗机构人员信息
	 * @param map
	 * @return
	 */
//	@CacheEvict(value={"queryPageYljgryxx"}, allEntries=true)
	public JSONObject saveYljgry(Map map) {
		ResultObject ro = new ResultObject();
		/*日期转换*/
		String value = (String)map.get("CSRQ");
		if(StringUtils.isEmpty(value)){
			map.put("CSRQ", null);
		}else{
			map.put("CSRQ", FormatDate.getDateY_M_D(value));
		}
		value = (String)map.get("BYRQ");
		if(StringUtils.isEmpty(value)){
			map.put("BYRQ", null);
		}else{
			map.put("BYRQ", FormatDate.getDateY_M_D(value));
		}
		value = (String)map.get("CJGZRQ");
		if(StringUtils.isEmpty(value)){
			map.put("CJGZRQ", null);
		}else{
			map.put("CJGZRQ", FormatDate.getDateY_M_D(value));
		}
		value = (String)map.get("DBDWRQ");
		if(StringUtils.isEmpty(value)){
			map.put("DBDWRQ", null);
		}else{
			map.put("DBDWRQ", FormatDate.getDateY_M_D(value));
		}

		try{
			MultipartFile RYZP = (MultipartFile)map.remove("RYZP");
			if(RYZP != null){
				String TPID = fileUtils.saveImageToMongoAndLocal(RYZP, "RYZP", new JSONObject(0));
				map.put("TPID", TPID);
			}
			String USERID = (String)map.get("USERID");
			if(StringUtils.isEmpty(USERID)){
				insert(map, ro);
			}else{
				update(map, ro);
			}
		}catch (Exception e){
			e.printStackTrace();
			ro.changeFaultState("保存人员信息失败");
			throw new RuntimeException("保存人员信息失败", e);
		}

		return (JSONObject)JSON.toJSON(ro);
	}

	/**
	 * 更新
	 * @param map
	 * @param ro
	 */
	private void update(Map map, ResultObject ro) {
		try {
			yljgryDAO.updateForPrimaryKey(map);
			yljgryZybjDAO.deleteForPrimaryKey(map);
			yljgryZybjDAO.insert(map);
			yljgryPracticeDAO.deleteForPrimaryKey(map);
			yljgryPracticeDAO.insert(map);
			String userId = (String)map.remove("USERID");
			String ddzyxx = (String)map.remove("ddzyxx");
			JSONArray ary = JSON.parseArray(ddzyxx);
			yljgryPracticeMultiDAO.deleteByUserId(userId);
			yljgryPracticeMultiDAO.insertBatch(userId, ary);
		} catch (Exception e) {
			ro.changeFaultState("更新医疗机构人员信息失败！");
			throw new RuntimeException("更新医疗机构人员信息失败！", e);
		}
	}
	
	/***
	 * 新增
	 * @param map
	 * @param ro
	 */
	private void insert(Map map, ResultObject ro) {
		try {
			if(yljgryDAO.isExist(map)){
				ro.changeFaultState("该医疗机构人员已存在！");
			}else{
				String ddzyxx = (String)map.remove("ddzyxx");
				String userId = "YLJG_"+UUID.randomUUID().toString();
				map.put("USERID", userId);
				yljgryDAO.insert(map);
				yljgryZybjDAO.insert(map);
				yljgryPracticeDAO.insert(map);
				JSONArray ary = JSON.parseArray(ddzyxx);
				yljgryPracticeMultiDAO.insertBatch(userId, ary);
			}

		} catch (Exception e) {
			ro.changeFaultState("新增医疗机构人员信息失败！");
			throw new RuntimeException("新增医疗机构人员信息失败！", e);
		}
	}

	/**
	 * 删除医疗机构人员信息
	 * 删除关联的用户信息
	 * @param map
	 * @return
	 */
//	@CacheEvict(value={"queryPageYljgryxx"}, allEntries=true)
	public JSONObject deleteYljgryxx(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		try {
			yljgryDAO.deleteForPrimaryKey(map);
			yljgryZybjDAO.deleteForPrimaryKey(map);
			yljgryPracticeDAO.deleteForPrimaryKey(map);
			yljgryPracticeMultiDAO.deleteForPrimaryKey(map);
			String userId = map.get("USERID");
			userDAO.deleteByUserId(userId);
		} catch (Exception e) {
			ro.changeFaultState("删除医疗机构人员失败！");
			throw new RuntimeException("删除医疗机构人员失败！", e);
		}
		return (JSONObject)JSON.toJSON(ro);
	}

	/**
	 * 查询当前用户可查询的机构信息
	 * @param map
	 * @return
	 */
	public JSONObject queryYljgxx(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		return (JSONObject)JSON.toJSON(ro);
	}

	@Override
	public JSONArray queryYljgryById(Map<String, String> map) {
		return yljgryDAO.queryYljgryById(map);
	}
	
	/**
	 * 将医疗机构人员信息导出到Excel
	 * @param map
	 * @param request
	 * @param response
	 */
	public void downloadToExcel(Map map, HttpServletRequest request, HttpServletResponse response){
		JSONArray dataArr = yljgryDAO.queryAll(map);
		JSONObject xbObj = JSONObject.parseObject("{'1':'男','2':'女'}");
		for(int i=0;i<dataArr.size();i++){
			JSONObject item = dataArr.getJSONObject(i);
			item.put("XB", xbObj.getString(item.get("XB").toString()));
		}
		//表头
		String titleStr = "{'YLJGMC':'医疗机构名称','KSMC':'科室名称','YSGH':'工号','XM':'姓名','XB':'性别'}";
		JSONObject title = JSONObject.parseObject(titleStr);
		dataArr.add(0, title);
		
		//排列循序
		List<String> titleField = new ArrayList<>();
		titleField.add("YLJGMC");
		titleField.add("KSMC");
		titleField.add("YSGH");
		titleField.add("XM");
		titleField.add("XB");

		ExcelOrWordUtils.downLoadExcelFile(dataArr, titleField, "机构人员列表", request, response);
	}
}




















